﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.SystemSettings
{
    public class OrganizationSetController : Controller
    {
        ServiceReference1.Service1Client wsdcser = new ServiceReference1.Service1Client();
        ZHHQSERVICE.Service1Client zhhq = new ZHHQSERVICE.Service1Client();
        //
        // GET: /OrganizationSet/

        public ActionResult Index()
        {
            return View();
        }

        #region 获取账户列表
        /// <summary>
        /// 获取商户列表
        /// </summary>
        /// <returns></returns>
        public ActionResult getMer(JqGridParam jqgridparam)
        {
            try
            {
                // 声明实体
                List<Model.View_Order_Organization> List = new List<View_Order_Organization>();
                Dictionary<string, string> Dic = new Dictionary<string, string>();
                // 调用服务接口
                string result = wsdcser.GetOrgListForm(jqgridparam.page, jqgridparam.rows,Dic, GetYZM._YZM, 1);
                // 将服务返回的结果进行优化
                JObject searcho = JObject.Parse(result);
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_Organization>.JsonList(jieguo);//解析返回的结果\
                    foreach (Model.View_Order_Organization item in List)
                    {
                        item.OrgWaltLastTime = DateTimeHelper.ToDate2(item.OrgWaltLastTime);
                        if (item.OrgWaltLastTime.Length == 14)
                        {
                            item.OrgWaltLastTime = DateTimeHelper.ToDate2(item.OrgWaltLastTime);
                        }
                    }
                    //List<Model.Order_Organization_T> newList = new List<Order_Organization_T>();
                    //foreach (Model.Order_Organization_T item in List)
                    //{
                    //    int a = item.OrgWaltLimit / 100;
                    //    item.OrgWaltLimit = a;
                    //    int b = item.OrgWaltBalance/100;
                    //    item.OrgWaltBalance = b;
                    //    newList.Add(item);
                    //    //int newimit = int.Parsitem.OrgWaltLimit.ToString());
                    //}
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 新增账户
        /// <summary>
        /// 新增商户
        /// </summary>
        /// <param name="org"></param>
        /// <returns></returns>
        public ActionResult addMer(Model.Order_Organization_T org)
        {
            try
            {
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                org.OrgWaltLastTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                org.OrgState = 1;
                org.OrgWaltLastOper = user.OperID;
                #region
                List<Model.View_Order_MerSet> List = new List<Model.View_Order_MerSet>();
                string result1 = wsdcser.GetOrder_Mer(GetYZM._YZM, 1);
                JObject searcho1 = JObject.Parse(result1);//将服务返回的结果进行优化
                string bs1 = searcho1["BS"].ToString();//获取返回结果是否正常标识
                var orgmer = "";
                if (bs1 == "1")//返回结果正常
                {
                    IList<JToken> jieguo1 = searcho1["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_MerSet>.JsonList(jieguo1);//解析返回的结果
                    string[] mo=org.OrgMers.Split(',');
                    var a = mo[0];
                    for (int i = 0; i < mo.Length;i++ )//判断获取的商户集合是否是存在系统商户集合
                    {
                        foreach (var it in List)
                        {
                            if (mo[i] == it.MerID)
                            {
                                if (orgmer != "" && orgmer != null)
                                {
                                    orgmer = orgmer + "," + mo[i];
                                }
                                else
                                {
                                    orgmer += mo[i];
                                }

                            }
                            
                          }
                       

                    }
                    string[] or = orgmer.Split(',');
                    if (or.Length != mo.Length)//取保存的一个商户是否存在系统商户表中，不存在就是商户集合错误
                    {
                        bs1 = "-2";
                        return Content(bs1);
                    }
                }
                
                #endregion
                    org.OrgMers = orgmer;
                    string json = org.ToJson();
                    string result = wsdcser.SetOrganization(user.OperID, json, 1, GetYZM._YZM, 1);
                    JObject jb = JObject.Parse(result);
                    string bs = jb["BS"].ToString();
                    if (bs == "1")
                    {
                        //return Content(jb["Msg"].ToString());
                        return Content(bs);
                    }
                    else
                    {
                        return Content(bs);
                    }
             
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 删除账户

        public ActionResult DeleteMer(Model.Order_Organization_T org)
        {
            try
            {
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string json = org.ToJson();
                string result = wsdcser.SetOrganization(user.OperID, json, 3, GetYZM._YZM, 1);
                JObject jb = JObject.Parse(result);
                string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(bs);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 修改账户
        /// <summary>
        /// 新增商户
        /// </summary>
        /// <param name="org"></param>
        /// <returns></returns>
        public ActionResult UpdateMer(Model.Order_Organization_T org)
        {
            try
            {
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                org.OrgWaltLastTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                org.OrgState = 1;
                org.OrgWaltLastOper = user.OperID;
                string json = org.ToJson();
                string result = wsdcser.SetOrganization(user.OperID, json, 2, GetYZM._YZM, 1);
                JObject jb = JObject.Parse(result);
                string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(bs);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 查询账户
        public ActionResult SearchMer(string OrgName, string OrgPerson,string OrgPhone, JqGridParam jqgridparam)
        {
            try
            {
                // 声明实体
                List<Model.Order_Organization_T> List = new List<Order_Organization_T>();
                Dictionary<string, string> Dic = new Dictionary<string, string>();
                Dic.Add("OrgName", OrgName);
                Dic.Add("OrgPerson", OrgPerson);
                Dic.Add("OrgPhone", OrgPhone);
                // 调用服务接口
                string result = wsdcser.GetOrgListForm(jqgridparam.page, jqgridparam.rows, Dic, GetYZM._YZM, 1);
                // 将服务返回的结果进行优化
                JObject searcho = JObject.Parse(result);
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.Order_Organization_T>.JsonList(jieguo);//解析返回的结果\
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 修改额度
        public ActionResult UpdateOrgWaltLimit(int OrgID, int OrgWaltLimit)
        {
            try
            {
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = wsdcser.SetOrganizationLimit(user.OperID, OrgID, OrgWaltLimit * 100, GetYZM._YZM, 1);
                JObject jb = JObject.Parse(result);
                string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(bs);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 商户集合
        /// <summary>
        /// 商户集合
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMer1()
        {
            try
            {
                List<Model.View_Order_MerSet> List = new List<Model.View_Order_MerSet>();
                string result = wsdcser.GetOrder_Mer(GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_MerSet>.JsonList(jieguo);//解析返回的结果
                    return Json(List, JsonRequestBehavior.AllowGet);
                    //return Content(List.TreeToJson());
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
       }
        #endregion
    }
}
